package by.averianov.discounta.webapp.page.tabs;

import java.util.Iterator;

import javax.inject.Inject;
import javax.persistence.metamodel.SingularAttribute;

import org.apache.wicket.Page;
import org.apache.wicket.extensions.ajax.markup.html.modal.ModalWindow.PageCreator;
import org.apache.wicket.extensions.markup.html.repeater.data.sort.OrderByBorder;
import org.apache.wicket.extensions.markup.html.repeater.data.sort.SortOrder;
import org.apache.wicket.extensions.markup.html.repeater.util.SortableDataProvider;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.navigation.paging.PagingNavigator;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.markup.repeater.Item;
import org.apache.wicket.markup.repeater.data.DataView;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.ResourceModel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import by.averianov.discounta.datamodel.Card;
import by.averianov.discounta.datamodel.Card_;
import by.averianov.discounta.services.UserService;
import by.averianov.discounta.webapp.page.administrator.Administrator;
import by.averianov.discounta.webapp.page.create.CreateCard;
import by.averianov.discounta.webapp.page.table.Row;

public class TabCard extends Panel {

	private static final Logger LOGGER = LoggerFactory.getLogger(TabCard.class);
	
	private Long selectId;
	private String currentMenu;
	
	@Inject
	private UserService userService;
	 
    public TabCard(String id, final Long selectId, String currentMenu) {
    	super(id);
    	this.selectId = selectId;
    	this.currentMenu = currentMenu;
	}

	private class CardDataProvider extends SortableDataProvider<Card, SingularAttribute<Card, ?>> {

		public CardDataProvider() {
			super();
			setSort(Card_.id, SortOrder.ASCENDING);
		}
	
		@Override
		public Iterator<? extends Card> iterator(long first, long count) {
	
			SingularAttribute<Card, ?> sortParam = getSort().getProperty();
			SortOrder propertySortOrder = getSortState().getPropertySortOrder(sortParam);
			boolean ascending = SortOrder.ASCENDING.equals(propertySortOrder);
			return userService.getAllCard(sortParam, ascending, (int) first, (int) count).iterator();
		}
	
		@Override
		public long size() {
			return userService.getCardCount();
		}
	
		@Override
		public IModel<Card> model(Card card) {
			return new CompoundPropertyModel<Card>(card);
		}
	}
    
    @Override
	protected void onInitialize() {
		super.onInitialize();
		
		Link linkAdd = new Link("linkAdd"){
			
			@Override
			public void onClick() {
				setResponsePage(new CreateCard(null, new PageCreator() {

					@Override
					public Page createPage() {
						return new Administrator(1, 0L);
					}
				}));
				LOGGER.debug("Add Row");
			}
		};
		linkAdd.add(new Label("labelAdd", getButtonAdd()));
		/*if (currentMenu == "priceBook") {
			linkAdd.setVisible(false);
		}*/
		add(linkAdd);

		Link linkChange = new Link("linkChange"){
			@Override
			public void onClick() {
				if(selectId == null) {
					selectId = 0L;
				}
				setResponsePage(new CreateCard(userService.getCardById(selectId), new PageCreator() {

					@Override
					public Page createPage() {
						return new Administrator(1, selectId);
					}
				}));
				LOGGER.debug("Change Row");
			}
		};
		linkChange.add(new Label("labelChange", getButtonChange()));
		/*if (currentMenu == "priceBook") {
			linkChange.setVisible(false);
		}*/
		add(linkChange);
		
		Link linkDel = new Link("linkDel"){
			@Override
			public void onClick() {
				Administrator page = new Administrator(1, 0L);				
				if(selectId == 0) {
					page.error("Null row selected");
					setResponsePage(page);
				} else {
					userService.removeCard(selectId);
					LOGGER.debug("Delete Row");					
				}
				setResponsePage(page);
			}
		};
		linkDel.add(new Label("labelDel", getButtonDel()));
		/*if (currentMenu == "priceBook") {
			linkAdd.setVisible(false);
			linkChange.setVisible(false);
			linkDel.setVisible(false);
		}*/
		add(linkDel);
		
		if (currentMenu == "priceBook") {
			linkAdd.setVisible(false);
			linkChange.setVisible(false);
			linkDel.setVisible(false);
		}

    	setOutputMarkupId(true);
    	CardDataProvider cardDataProvider = new CardDataProvider();

		final WebMarkupContainer tableBody = new WebMarkupContainer("wrapper-body") {
			@Override
			protected void onBeforeRender() {
				super.onBeforeRender();
			}
		};
	
		tableBody.setOutputMarkupId(true);
		add(tableBody);
	
		DataView<Card> dataView = new DataView<Card>("list", cardDataProvider, 15) {

			@Override
			protected void populateItem(Item<Card> item) {
				final Card card = item.getModelObject();
                Boolean isSelected = false;
				
				if(selectId == card.getId()) {
					isSelected = true;
				}
				
				item.add(new Row("row", card, isSelected, currentMenu));
				item.setVisible(true);                				
			}
			
		};
	
		tableBody.add(dataView);	
		add(new PagingNavigator("paging", dataView));	
		add(new OrderByBorder<SingularAttribute<Card, ?>>("sortById", Card_.id, cardDataProvider));
		add(new OrderByBorder<SingularAttribute<Card, ?>>("sortByType", Card_.cardType, cardDataProvider));
		add(new OrderByBorder<SingularAttribute<Card, ?>>("sortByStatus", Card_.cardStatus, cardDataProvider));
		add(new OrderByBorder<SingularAttribute<Card, ?>>("sortByUserProfile", Card_.userProfile, cardDataProvider));
		add(new OrderByBorder<SingularAttribute<Card, ?>>("sortByUserEditor", Card_.userEditor, cardDataProvider));
		add(new OrderByBorder<SingularAttribute<Card, ?>>("sortByDateCreate", Card_.cardDateCreate, cardDataProvider));
		add(new OrderByBorder<SingularAttribute<Card, ?>>("sortByDateStart", Card_.cardDateStart, cardDataProvider));
		
		LOGGER.debug("onInitialize table");		
	}
    

	protected IModel<String> getButtonAdd() {
		return new ResourceModel("p.button.add");
	}
	
	protected IModel<String> getButtonChange() {
		return new ResourceModel("p.button.change");
	}

	protected IModel<String> getButtonDel() {
		return new ResourceModel("p.button.del");
	}

}